{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The best front for Generation number  0  is\n",
      "1.782 \n",
      "\n",
      "The best front for Generation number  1  is\n",
      "1.782 \n",
      "\n",
      "The best front for Generation number  2  is\n",
      "1.782 \n",
      "\n",
      "The best front for Generation number  3  is\n",
      "-0.625 1.782 \n",
      "\n",
      "The best front for Generation number  4  is\n",
      "1.782 -0.625 \n",
      "\n",
      "The best front for Generation number  5  is\n",
      "1.673 1.782 -0.625 1.799 \n",
      "\n",
      "The best front for Generation number  6  is\n",
      "1.307 1.673 1.782 0.903 -0.625 1.799 \n",
      "\n",
      "The best front for Generation number  7  is\n",
      "1.799 1.307 1.673 -0.625 1.782 0.903 \n",
      "\n",
      "The best front for Generation number  8  is\n",
      "0.903 1.799 1.307 1.782 1.673 -0.625 \n",
      "\n",
      "The best front for Generation number  9  is\n",
      "2.077 0.903 1.799 1.673 -0.625 1.782 1.307 \n",
      "\n",
      "The best front for Generation number  10  is\n",
      "1.307 2.077 0.903 -0.625 1.782 1.673 1.799 \n",
      "\n",
      "The best front for Generation number  11  is\n",
      "0.476 1.307 2.077 1.673 1.799 1.782 0.903 \n",
      "\n",
      "The best front for Generation number  12  is\n",
      "0.903 0.476 1.307 1.799 1.782 2.077 1.673 \n",
      "\n",
      "The best front for Generation number  13  is\n",
      "1.196 0.903 0.476 1.307 2.077 1.673 1.799 1.782 \n",
      "\n",
      "The best front for Generation number  14  is\n",
      "0.843 1.196 0.903 1.307 1.673 0.476 1.799 1.782 2.077 \n",
      "\n",
      "The best front for Generation number  15  is\n",
      "0.613 0.843 1.196 0.903 1.307 1.673 1.782 0.476 1.799 2.077 \n",
      "\n",
      "The best front for Generation number  16  is\n",
      "0.332 0.613 0.903 1.673 0.843 1.196 1.307 0.476 1.799 1.782 2.077 \n",
      "\n",
      "The best front for Generation number  17  is\n",
      "2.077 0.332 1.673 1.196 0.613 0.903 0.843 0.476 1.799 1.307 1.782 \n",
      "\n",
      "The best front for Generation number  18  is\n",
      "1.782 2.077 1.196 0.332 0.903 1.673 0.613 0.476 1.799 0.843 1.307 \n",
      "\n",
      "The best front for Generation number  19  is\n",
      "1.307 1.782 0.332 2.077 1.673 1.196 0.903 0.476 1.799 0.613 0.843 \n",
      "\n",
      "The best front for Generation number  20  is\n",
      "1.227 1.307 1.673 1.782 0.332 0.903 0.476 1.196 0.613 1.935 0.843 1.799 \n",
      "\n",
      "The best front for Generation number  21  is\n",
      "1.811 1.227 1.307 1.799 1.673 0.903 1.782 0.332 0.843 0.476 1.935 1.196 0.613 \n",
      "\n",
      "The best front for Generation number  22  is\n",
      "0.949 1.811 1.227 1.673 0.332 1.782 0.903 0.613 1.307 1.799 1.196 1.935 0.476 0.843 \n",
      "\n",
      "The best front for Generation number  23  is\n",
      "0.983 0.949 1.811 1.307 0.613 1.799 1.196 0.332 0.903 1.227 1.935 1.782 1.673 0.109 0.795 0.476 0.843 0.89 \n",
      "\n",
      "The best front for Generation number  24  is\n",
      "0.89 0.983 0.903 0.332 1.227 0.949 1.935 1.196 1.782 0.613 1.799 1.811 0.843 1.307 0.476 1.673 0.109 0.795 \n",
      "\n",
      "The best front for Generation number  25  is\n",
      "0.795 0.89 1.782 1.196 0.613 0.983 1.799 1.935 1.811 1.227 0.949 0.903 0.109 0.332 1.673 0.843 1.307 0.476 \n",
      "\n",
      "The best front for Generation number  26  is\n",
      "0.476 0.795 1.811 1.935 1.227 0.949 0.89 1.799 0.903 0.613 0.983 1.782 1.307 0.843 1.196 0.109 0.332 1.673 \n",
      "\n",
      "The best front for Generation number  27  is\n",
      "1.673 0.476 0.903 1.799 0.613 0.795 0.983 0.89 1.782 1.227 0.949 1.811 0.332 0.109 1.935 1.307 0.843 1.196 \n",
      "\n",
      "The best front for Generation number  28  is\n",
      "1.196 1.673 0.476 1.782 0.89 1.227 0.949 0.983 0.613 1.811 0.903 0.795 1.799 0.843 1.307 0.332 0.109 1.935 \n",
      "\n",
      "The best front for Generation number  29  is\n",
      "1.78 1.196 1.673 0.613 0.903 1.811 0.983 0.795 0.476 0.949 1.227 1.935 0.109 1.799 1.782 0.89 0.332 1.307 0.843 \n",
      "\n",
      "The best front for Generation number  30  is\n",
      "0.843 1.78 1.196 0.476 1.227 1.673 0.795 0.949 0.983 1.935 1.811 0.613 0.903 0.332 1.307 0.109 0.89 1.799 1.782 \n",
      "\n",
      "The best front for Generation number  31  is\n",
      "1.782 0.843 1.78 0.983 1.811 1.935 0.949 0.613 1.196 0.795 1.673 1.799 0.89 0.903 0.476 1.227 0.109 1.307 0.332 \n",
      "\n",
      "The best front for Generation number  32  is\n",
      "0.332 1.782 0.843 1.196 1.673 0.795 0.613 1.799 1.78 0.949 1.935 1.307 0.109 0.89 0.983 1.811 1.227 0.476 0.903 \n",
      "\n",
      "The best front for Generation number  33  is\n",
      "0.903 0.332 1.782 1.78 1.935 0.949 1.799 1.307 0.843 0.613 0.795 0.476 1.227 1.196 0.109 1.673 1.811 0.983 0.89 \n",
      "\n",
      "The best front for Generation number  34  is\n",
      "2.039 0.903 0.332 0.613 0.795 0.843 0.476 1.782 1.307 1.799 0.89 1.78 0.983 1.227 1.811 1.673 1.935 1.196 0.949 0.109 \n",
      "\n",
      "The best front for Generation number  35  is\n",
      "0.109 2.039 0.903 1.799 0.89 1.307 1.78 0.332 1.782 0.476 0.949 0.613 1.196 0.983 1.935 0.795 1.673 1.227 0.843 1.811 \n",
      "\n",
      "The best front for Generation number  36  is\n",
      "1.811 0.109 2.039 0.476 0.949 1.782 0.613 0.903 0.332 1.78 0.843 1.799 1.227 1.196 1.673 0.795 0.983 0.89 1.307 1.935 \n",
      "\n",
      "The best front for Generation number  37  is\n",
      "0.446 1.811 0.109 1.78 1.799 2.039 0.332 0.843 0.903 1.227 0.476 0.613 0.949 1.782 0.89 1.307 1.196 1.935 0.983 1.673 \n",
      "\n",
      "The best front for Generation number  38  is\n",
      "0.867 0.446 1.811 0.476 0.949 0.613 1.227 1.782 0.109 1.673 0.903 0.843 1.78 1.65 0.983 2.039 1.799 0.497 0.89 0.332 \n",
      "\n",
      "The best front for Generation number  39  is\n",
      "0.332 0.867 0.903 1.673 0.109 0.843 0.446 1.227 1.782 1.811 0.497 1.799 0.949 1.78 0.476 1.65 2.039 0.613 0.89 0.983 \n",
      "\n",
      "The best front for Generation number  40  is\n",
      "1.356 0.332 0.867 0.497 1.799 1.811 0.903 0.949 1.782 0.109 0.446 1.673 0.843 1.78 1.227 0.983 0.89 0.613 1.65 0.476 \n",
      "\n",
      "The best front for Generation number  41  is\n",
      "0.476 1.356 0.109 0.446 0.332 1.782 1.673 0.949 1.811 0.903 0.843 0.867 0.497 1.799 1.65 0.613 0.89 1.78 1.227 0.983 \n",
      "\n",
      "The best front for Generation number  42  is\n",
      "1.269 0.476 1.356 0.843 1.811 0.903 1.782 0.867 0.497 1.673 0.109 0.446 0.949 0.332 1.799 0.983 1.227 1.78 1.65 0.613 \n",
      "\n",
      "The best front for Generation number  43  is\n",
      "0.613 1.269 0.476 0.497 1.673 0.903 0.109 0.446 1.782 1.356 0.843 0.867 1.811 0.949 1.65 1.78 1.227 0.332 1.799 0.983 \n",
      "\n",
      "The best front for Generation number  44  is\n",
      "1.867 0.613 1.269 1.356 0.867 0.109 0.843 1.782 0.476 1.799 0.983 1.811 0.332 1.673 0.446 0.497 1.227 0.903 1.65 0.949 \n",
      "\n",
      "The best front for Generation number  45  is\n",
      "0.24 1.867 0.983 0.613 0.109 0.476 1.782 1.799 0.843 1.811 0.332 1.356 1.269 1.673 0.867 0.497 0.949 1.65 0.903 1.227 \n",
      "\n",
      "The best front for Generation number  46  is\n",
      "1.227 0.24 1.867 0.843 0.613 0.983 1.811 0.476 0.332 1.782 1.356 1.799 0.109 1.269 0.903 1.65 0.949 1.673 0.867 0.497 \n",
      "\n",
      "The best front for Generation number  47  is\n",
      "-0.068 1.227 0.24 1.356 1.867 0.983 0.332 1.782 0.476 1.799 1.811 0.109 0.843 0.497 1.269 0.867 0.613 1.65 1.673 0.949 \n",
      "\n",
      "The best front for Generation number  48  is\n",
      "0.949 -0.068 1.227 0.24 0.983 1.356 1.782 0.332 1.799 0.476 1.867 1.811 0.109 0.843 1.673 1.65 1.269 0.613 0.867 0.497 \n",
      "\n",
      "The best front for Generation number  49  is\n",
      "0.497 0.949 -0.068 1.227 1.356 0.332 0.24 1.782 0.476 1.799 1.867 1.811 0.983 0.867 0.109 0.613 1.673 1.269 1.65 0.843 \n",
      "\n",
      "The best front for Generation number  50  is\n",
      "0.843 0.497 0.949 -0.068 0.332 1.227 0.24 1.782 1.356 0.476 1.799 1.867 1.811 0.983 1.65 1.269 0.109 1.673 0.613 0.867 \n",
      "\n",
      "The best front for Generation number  51  is\n",
      "0.608 0.843 0.497 0.24 1.227 0.949 1.356 1.799 0.476 1.782 -0.068 1.867 1.315 1.811 0.983 0.867 1.65 0.332 1.269 0.109 \n",
      "\n",
      "The best front for Generation number  52  is\n",
      "0.752 0.608 0.843 -0.068 1.227 1.356 0.476 0.949 1.315 0.983 1.782 1.799 0.497 1.867 0.24 1.811 0.109 1.269 0.867 1.65 \n",
      "\n",
      "The best front for Generation number  53  is\n",
      "1.65 0.752 0.608 0.843 -0.068 1.227 1.356 0.476 1.315 0.983 1.782 0.949 1.799 1.867 0.497 0.867 0.24 0.109 1.269 1.811 \n",
      "\n",
      "The best front for Generation number  54  is\n",
      "1.811 1.65 0.752 0.608 -0.068 0.843 1.227 1.356 0.983 1.315 1.782 0.949 0.476 1.269 1.867 1.799 0.24 0.109 0.497 0.867 \n",
      "\n",
      "The best front for Generation number  55  is\n",
      "0.867 1.811 1.65 -0.068 0.752 0.608 0.843 1.315 0.983 1.227 1.782 0.949 0.497 1.356 1.269 0.476 0.24 0.109 1.867 1.799 \n",
      "\n",
      "The best front for Generation number  56  is\n",
      "1.641 0.867 1.811 1.65 -0.068 0.608 1.227 0.843 1.315 0.949 0.752 0.983 1.799 1.356 1.782 0.497 0.109 1.867 0.476 1.269 \n",
      "\n",
      "The best front for Generation number  57  is\n",
      "0.153 1.641 0.867 1.65 1.811 0.608 0.949 1.227 0.843 0.983 1.269 -0.068 1.315 1.356 0.752 1.799 1.867 0.497 0.476 1.782 \n",
      "\n",
      "The best front for Generation number  58  is\n",
      "1.782 0.153 1.641 0.867 0.608 0.843 1.227 -0.068 0.983 0.497 1.315 1.65 0.949 1.269 0.476 1.811 0.752 1.867 1.356 1.799 \n",
      "\n",
      "The best front for Generation number  59  is\n",
      "1.799 1.782 0.153 1.641 0.843 0.983 1.65 1.867 -0.068 0.497 0.949 1.315 1.227 0.867 1.356 0.608 0.476 0.752 1.269 1.811 \n",
      "\n",
      "The best front for Generation number  60  is\n",
      "1.811 1.799 1.782 0.153 0.983 -0.068 0.752 1.315 1.867 0.497 1.227 0.949 1.65 1.641 1.269 0.843 1.356 0.476 0.867 0.608 \n",
      "\n",
      "The best front for Generation number  61  is\n",
      "0.509 1.811 1.799 0.497 -0.068 1.782 0.949 1.315 0.752 1.641 1.867 1.65 1.227 0.153 0.867 0.843 0.608 0.983 0.476 1.269 \n",
      "\n",
      "The best front for Generation number  62  is\n",
      "0.792 0.509 1.811 1.867 0.752 1.799 1.782 1.641 1.227 1.65 0.949 1.315 0.153 0.497 -0.068 0.843 0.867 0.476 0.983 1.269 \n",
      "\n",
      "The best front for Generation number  63  is\n",
      "1.269 0.792 0.509 1.65 1.227 0.949 1.799 1.315 1.811 1.641 0.153 1.782 1.867 0.476 0.983 -0.068 0.867 0.752 0.497 0.843 \n",
      "\n",
      "The best front for Generation number  64  is\n",
      "1.999 1.269 0.792 0.153 1.641 1.799 1.782 0.509 1.867 1.811 0.476 1.65 1.315 1.227 0.497 0.31 -0.068 0.752 0.843 0.949 \n",
      "\n",
      "The best front for Generation number  65  is\n",
      "0.949 1.999 1.269 1.782 0.792 1.867 1.811 1.65 0.476 0.509 0.153 1.641 0.843 -0.068 1.227 0.31 0.752 1.799 1.315 0.497 \n",
      "\n",
      "The best front for Generation number  66  is\n",
      "0.497 0.949 1.999 1.269 1.811 1.782 0.476 0.792 0.509 1.65 0.153 1.641 1.867 -0.068 0.752 0.31 1.315 1.799 0.843 1.227 \n",
      "\n",
      "The best front for Generation number  67  is\n",
      "0.614 0.497 0.949 1.65 1.641 0.476 1.999 0.509 0.792 0.153 1.867 1.269 1.811 0.752 1.782 1.799 1.227 1.315 0.843 -0.068 \n",
      "\n",
      "The best front for Generation number  68  is\n",
      "1.051 0.614 0.497 0.949 0.153 1.867 1.999 1.811 1.269 0.792 0.752 0.509 1.641 0.476 1.65 1.799 -0.068 0.843 1.782 1.315 \n",
      "\n",
      "The best front for Generation number  69  is\n",
      "1.315 1.051 0.614 1.269 0.792 0.497 0.752 0.509 1.999 1.811 0.153 1.867 0.843 0.476 -0.068 0.949 1.641 1.799 1.782 1.65 \n",
      "\n",
      "The best front for Generation number  70  is\n",
      "1.65 1.315 1.051 0.614 1.999 1.811 0.752 0.153 1.867 0.509 0.792 0.497 1.269 0.476 1.799 1.641 0.843 0.949 1.782 -0.068 \n",
      "\n",
      "The best front for Generation number  71  is\n",
      "-0.068 1.65 1.315 1.051 1.867 0.509 0.752 0.792 0.497 0.153 1.999 1.811 0.614 0.476 0.949 0.843 1.269 1.641 1.782 1.799 \n",
      "\n",
      "The best front for Generation number  72  is\n",
      "0.69 -0.068 1.65 1.999 0.497 0.153 1.315 1.811 0.614 0.752 0.792 1.867 0.509 1.051 0.949 0.476 1.782 1.641 1.269 0.843 \n",
      "\n",
      "The best front for Generation number  73  is\n",
      "0.843 0.69 0.752 0.792 -0.068 0.614 1.811 1.867 0.509 0.153 1.65 1.315 0.949 1.999 1.051 0.497 1.782 1.641 0.476 1.269 \n",
      "\n",
      "The best front for Generation number  74  is\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.269 0.843 0.153 1.65 0.69 0.509 1.867 1.315 0.949 0.614 0.752 1.811 1.051 0.792 1.999 -0.068 1.782 1.641 0.497 0.476 \n",
      "\n",
      "The best front for Generation number  75  is\n",
      "0.476 1.269 0.614 0.752 0.843 0.949 1.315 1.811 1.051 0.509 0.153 1.867 1.999 1.65 0.792 0.69 1.782 1.641 -0.068 0.497 \n",
      "\n",
      "The best front for Generation number  76  is\n",
      "1.006 0.476 0.153 0.509 1.269 1.867 1.811 1.051 1.65 1.999 0.949 0.614 1.315 0.69 0.752 0.792 0.843 1.641 -0.068 1.782 \n",
      "\n",
      "The best front for Generation number  77  is\n",
      "1.782 1.006 0.476 1.999 0.949 1.051 1.65 1.315 0.614 0.153 1.867 1.811 0.509 0.752 1.641 0.69 1.269 0.843 -0.068 0.792 \n",
      "\n",
      "The best front for Generation number  78  is\n",
      "0.792 1.782 1.006 0.153 1.867 1.315 0.614 0.509 1.811 0.476 1.051 1.65 1.999 1.641 0.843 0.949 0.752 1.269 -0.068 0.69 \n",
      "\n",
      "The best front for Generation number  79  is\n",
      "0.69 0.792 1.782 0.476 1.051 0.509 1.811 1.999 1.65 1.006 1.315 0.614 0.153 1.867 0.843 1.641 1.269 0.752 0.949 -0.068 \n",
      "\n",
      "The best front for Generation number  80  is\n",
      "1.919 0.69 0.792 1.782 1.315 1.65 1.006 0.614 0.153 1.811 1.999 1.051 0.476 0.509 0.843 1.867 0.949 1.269 -0.068 0.752 \n",
      "\n",
      "The best front for Generation number  81  is\n",
      "0.752 1.919 0.69 1.811 1.999 0.614 0.153 0.792 0.476 1.051 1.65 1.006 1.315 1.782 0.509 1.269 0.843 1.867 -0.068 0.949 \n",
      "\n",
      "The best front for Generation number  82  is\n",
      "0.949 0.752 1.919 1.051 0.792 1.65 0.69 0.476 1.315 1.006 0.614 0.153 1.999 1.811 1.782 0.509 1.867 1.269 -0.068 0.843 \n",
      "\n",
      "The best front for Generation number  83  is\n",
      "0.843 0.949 0.752 1.006 0.614 0.476 1.315 1.999 0.153 1.919 1.65 0.69 0.792 1.811 1.269 1.051 1.782 -0.068 0.509 1.867 \n",
      "\n",
      "The best front for Generation number  84  is\n",
      "1.867 0.843 1.919 0.949 1.65 1.999 0.153 0.792 0.69 0.752 0.476 1.315 1.811 0.614 -0.068 1.269 1.006 0.509 1.051 1.782 \n",
      "\n",
      "The best front for Generation number  85  is\n",
      "1.782 1.867 0.752 0.476 0.843 0.792 0.69 1.811 1.315 1.919 1.999 0.153 0.614 1.65 0.509 -0.068 0.949 1.051 1.269 1.006 \n",
      "\n",
      "The best front for Generation number  86  is\n",
      "1.006 1.782 1.919 1.999 1.867 1.811 1.315 0.614 0.153 0.752 0.792 0.69 1.65 0.843 1.051 0.509 0.476 1.269 -0.068 0.949 \n",
      "\n",
      "The best front for Generation number  87  is\n",
      "0.949 1.006 0.752 1.782 0.792 0.614 0.153 1.65 0.69 1.919 1.811 1.315 0.843 1.867 1.269 1.051 1.999 -0.068 0.509 0.476 \n",
      "\n",
      "The best front for Generation number  88  is\n",
      "0.476 0.949 1.006 1.919 1.65 1.811 0.752 0.69 0.843 1.315 0.614 0.153 0.792 1.782 1.867 -0.068 1.269 1.051 0.509 1.999 \n",
      "\n",
      "The best front for Generation number  89  is\n",
      "1.999 0.476 0.949 1.315 0.69 0.614 1.006 0.843 0.792 1.811 0.153 0.752 1.65 1.919 1.782 1.867 1.051 -0.068 0.509 1.269 \n",
      "\n",
      "The best front for Generation number  90  is\n",
      "1.269 1.999 0.476 1.811 0.153 0.843 0.792 1.65 0.752 0.949 0.614 1.006 0.69 1.919 -0.068 1.315 1.782 0.509 1.867 1.051 \n",
      "\n",
      "The best front for Generation number  91  is\n",
      "1.051 1.269 1.999 0.949 0.614 1.65 0.752 0.476 0.69 1.006 0.843 0.792 0.153 1.811 1.919 0.509 -0.068 1.315 1.867 1.782 \n",
      "\n",
      "The best front for Generation number  92  is\n",
      "1.782 1.051 1.006 0.843 1.269 0.476 0.69 0.153 0.792 1.999 1.65 0.752 1.811 0.614 1.315 1.919 0.949 1.867 0.509 -0.068 \n",
      "\n",
      "The best front for Generation number  93  is\n",
      "-0.068 1.782 1.999 1.051 1.65 0.153 0.792 1.811 0.752 1.006 0.476 0.69 0.614 1.269 1.867 1.315 0.843 0.509 1.919 0.949 \n",
      "\n",
      "The best front for Generation number  94  is\n",
      "0.949 -0.068 1.006 1.782 0.476 1.811 0.752 0.614 0.69 1.999 0.153 0.792 1.269 1.65 0.509 1.867 1.051 1.919 1.315 0.843 \n",
      "\n",
      "The best front for Generation number  95  is\n",
      "0.843 0.949 -0.068 1.999 0.153 0.614 0.69 1.006 1.269 0.792 1.811 0.752 0.476 1.782 1.65 1.919 0.509 1.867 1.315 1.051 \n",
      "\n",
      "The best front for Generation number  96  is\n",
      "1.051 0.843 0.792 0.949 1.811 1.006 1.269 0.476 0.752 -0.068 0.614 0.69 1.782 0.153 1.867 1.65 1.999 1.315 1.919 0.509 \n",
      "\n",
      "The best front for Generation number  97  is\n",
      "0.509 1.051 -0.068 0.843 0.614 0.476 0.752 1.782 0.69 0.792 1.006 1.269 0.153 1.811 1.315 1.867 0.949 1.919 1.65 1.999 \n",
      "\n",
      "The best front for Generation number  98  is\n",
      "1.999 0.509 0.792 1.006 1.051 1.782 0.69 0.153 1.269 -0.068 0.476 0.752 1.811 0.614 1.919 1.315 0.843 1.65 1.867 0.949 \n",
      "\n",
      "The best front for Generation number  99  is\n",
      "0.949 1.999 0.509 -0.068 0.476 0.153 1.269 0.792 1.811 0.752 1.782 0.69 1.051 1.006 0.614 1.65 1.919 1.867 1.315 0.843 \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Importing required modules\n",
    "import math\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    " \n",
    "#定义函数1\n",
    "def function1(x):\n",
    "    value = -x**2\n",
    "    return value\n",
    " \n",
    "#定义函数2\n",
    "def function2(x):\n",
    "    value = -(x-2)**2\n",
    "    return value\n",
    " \n",
    "#Function to find index of list\n",
    "#查找列表指定元素的索引\n",
    "def index_of(a,list):\n",
    "    for i in range(0,len(list)):\n",
    "        if list[i] == a:\n",
    "            return i\n",
    "    return -1\n",
    " \n",
    "#Function to sort by values\n",
    "# 函数根据指定的值列表排序\n",
    "'''list1=[1,2,3,4,5,6,7,8,9]\n",
    "   value=[1,5,6,7]\n",
    "   sort_list=[1,5,6,7]\n",
    "'''\n",
    "def sort_by_values(list1, values):\n",
    "    sorted_list = []\n",
    "    while(len(sorted_list)!=len(list1)):\n",
    "        # 当结果长度不等于初始长度时，继续循环\n",
    "        if index_of(min(values),values) in list1:\n",
    "            # 标定值中最小值在目标列表中时\n",
    "            sorted_list.append(index_of(min(values),values))\n",
    "        #     将标定值的最小值的索引追加到结果列表后面\n",
    "        values[index_of(min(values),values)] = math.inf\n",
    "    #      将标定值的最小值置为无穷小,即删除原来的最小值,移向下一个\n",
    "    #     infinited\n",
    "    return sorted_list\n",
    " \n",
    "#Function to carry out NSGA-II's fast non dominated sort\n",
    "#函数执行NSGA-II的快速非支配排序,将所有的个体都分层\n",
    "'''\n",
    "郭军p21\n",
    "1.np=0 sp=infinite\n",
    "2.对所有个体进行非支配判断，若p支配q，则将q加入到sp中，并将q的层级提升一级。\n",
    "  若q支配p，将p加入sq中，并将p的层级提升一级。\n",
    "3.对种群当前分层序号k进行初始化，令k=1\n",
    "4.找出种群中np=0的个体，将其从种群中移除，将其加入到分层集合fk中，该集合就是层级为0个体的集合。\n",
    "5.判断fk是否为空，若不为空，将fk中所有的个体sp中对应的个体层级减去1，且k=k+1,跳到2;\n",
    "  若为空，则表明得到了所有非支配集合，程序结束\n",
    "'''\n",
    "\"\"\"基于序列和拥挤距离,这里找到任意两个个体p,q\"\"\"\n",
    "def fast_non_dominated_sort(values1, values2):\n",
    "    S=[[] for i in range(0,len(values1))]\n",
    "    # 种群中所有个体的sp进行初始化 这里的len(value1)=pop_size\n",
    "    front = [[]]\n",
    "    # 分层集合,二维列表中包含第n个层中,有那些个体\n",
    "    n=[0 for i in range(0,len(values1))]\n",
    "    rank = [0 for i in range(0, len(values1))]\n",
    "    # 评级\n",
    " \n",
    "    for p in range(0,len(values1)):\n",
    "        S[p]=[]\n",
    "        n[p]=0\n",
    "        # 寻找第p个个体和其他个体的支配关系\n",
    "        # 将第p个个体的sp和np初始化\n",
    "        for q in range(0, len(values1)):\n",
    "             #step2:p > q 即如果p支配q,则\n",
    "            if (values1[p] > values1[q] and values2[p] > values2[q]) or (values1[p] >= values1[q] and values2[p] > values2[q]) or (values1[p] > values1[q] and values2[p] >= values2[q]):\n",
    "            #支配判定条件:当且仅当,对于任取i属于{1,2},都有fi(p)>fi(q),符合支配.或者当且仅当对于任意i属于{1,2},有fi(p)>=fi(q),且至少存在一个j使得fj(p)>f(q)  符合弱支配\n",
    "                if q not in S[p]:\n",
    "                    # 同时如果q不属于sp将其添加到sp中\n",
    "                    S[p].append(q)\n",
    "            # 如果q支配p\n",
    "            elif (values1[q] > values1[p] and values2[q] > values2[p]) or (values1[q] >= values1[p] and values2[q] > values2[p]) or (values1[q] > values1[p] and values2[q] >= values2[p]):\n",
    "                # 则将np+1\n",
    "                n[p] = n[p] + 1\n",
    "        if n[p]==0:\n",
    "            # 找出种群中np=0的个体\n",
    "            rank[p] = 0\n",
    "            # 将其从pt中移去\n",
    "            if p not in front[0]:\n",
    "                # 如果p不在第0层中\n",
    "                # 将其追加到第0层中\n",
    "                front[0].append(p)\n",
    " \n",
    "    i = 0\n",
    "    while(front[i] != []):\n",
    "        # 如果分层集合为不为空，\n",
    "        Q=[]\n",
    "        for p in front[i]:\n",
    "            for q in S[p]:\n",
    "                n[q] =n[q] - 1\n",
    "                # 则将fk中所有给对应的个体np-1\n",
    "                if( n[q]==0):\n",
    "                    # 如果nq==0\n",
    "                    rank[q]=i+1\n",
    " \n",
    "                    if q not in Q:\n",
    "                        Q.append(q)\n",
    "        i = i+1\n",
    "        # 并且k+1\n",
    "        front.append(Q)\n",
    " \n",
    "    del front[len(front)-1]\n",
    " \n",
    "    return front\n",
    "    # 返回将所有个体分层后的结果\n",
    "#Function to calculate crowding distance\n",
    "#计算拥挤距离的函数\n",
    "'''\n",
    "高媛p29\n",
    "1.I[1]=I[l]=inf，I[i]=0 将边界的两个个体拥挤度设为无穷。\n",
    "2.I=sort(I,m)，基于目标函数m对种群排序\n",
    "3.I[i]=I[i]+(Im[i+1]-Im[i-1])/(fmax-fmin)\n",
    "'''\n",
    "def crowding_distance(values1, values2, front):\n",
    "    distance = [0 for i in range(0,len(front))]\n",
    "    # 初始化个体间的拥挤距离\n",
    "    sorted1 = sort_by_values(front, values1[:])\n",
    "    sorted2 = sort_by_values(front, values2[:])\n",
    "    # 基于目标函数1和目标函数2对已经划分好层级的种群排序\n",
    "    distance[0] = 4444444444444444\n",
    "    distance[len(front) - 1] = 4444444444444444\n",
    "    for k in range(1,len(front)-1):\n",
    "        distance[k] = distance[k]+ (values1[sorted1[k+1]] - values2[sorted1[k-1]])/(max(values1)-min(values1))\n",
    "    for k in range(1,len(front)-1):\n",
    "        distance[k] = distance[k]+ (values1[sorted2[k+1]] - values2[sorted2[k-1]])/(max(values2)-min(values2))\n",
    "    return distance\n",
    "#     返回拥挤距离\n",
    " \n",
    "#函数进行交叉\n",
    "def crossover(a,b):\n",
    "    r=random.random()\n",
    "    if r>0.5:\n",
    "        return mutation((a+b)/2)\n",
    "    else:\n",
    "        return mutation((a-b)/2)\n",
    "#函数进行变异操作\n",
    "def mutation(solution):\n",
    "    mutation_prob = random.random()\n",
    "    if mutation_prob <1:\n",
    "        solution = min_x+(max_x-min_x)*random.random()\n",
    "    return solution\n",
    " \n",
    "pop_size = 20\n",
    "max_gen = 100\n",
    "# 迭代次数\n",
    "#Initialization\n",
    "min_x=-55\n",
    "max_x=55\n",
    "solution=[min_x+(max_x-min_x)*random.random() for i in range(0,pop_size)]\n",
    "# 随机生成变量\n",
    "gen_no=0\n",
    "while(gen_no<max_gen):\n",
    "    function1_values = [function1(solution[i])for i in range(0,pop_size)]\n",
    "    function2_values = [function2(solution[i])for i in range(0,pop_size)]\n",
    "    # 生成两个函数值列表，构成一个种群\n",
    "    non_dominated_sorted_solution = fast_non_dominated_sort(function1_values[:],function2_values[:])\n",
    "    # 种群之间进行快速非支配性排序,得到非支配性排序集合\n",
    "    print(\"The best front for Generation number \",gen_no, \" is\")\n",
    "    for valuez in non_dominated_sorted_solution[0]:\n",
    "        print(round(solution[valuez],3),end=\" \")\n",
    "    print(\"\\n\")\n",
    "    crowding_distance_values=[]\n",
    "    # 计算非支配集合中每个个体的拥挤度\n",
    "    for i in range(0,len(non_dominated_sorted_solution)):\n",
    "        crowding_distance_values.append(crowding_distance(function1_values[:],function2_values[:],non_dominated_sorted_solution[i][:]))\n",
    "    solution2 = solution[:]\n",
    " \n",
    "    #生成了子代\n",
    "    while(len(solution2)!=2*pop_size):\n",
    "        a1 = random.randint(0,pop_size-1)\n",
    "        b1 = random.randint(0,pop_size-1)\n",
    "        # 选择\n",
    "        solution2.append(crossover(solution[a1],solution[b1]))\n",
    "        #随机选择，将种群中的个体进行交配，得到子代种群2*pop_size\n",
    "        \n",
    "    function1_values2 = [function1(solution2[i])for i in range(0,2*pop_size)]\n",
    "    function2_values2 = [function2(solution2[i])for i in range(0,2*pop_size)]\n",
    "    non_dominated_sorted_solution2 = fast_non_dominated_sort(function1_values2[:],function2_values2[:])\n",
    "    # 将两个目标函数得到的两个种群值value,再进行排序 得到2*pop_size解\n",
    "    crowding_distance_values2=[]\n",
    "    for i in range(0,len(non_dominated_sorted_solution2)):\n",
    "        crowding_distance_values2.append(crowding_distance(function1_values2[:],function2_values2[:],non_dominated_sorted_solution2[i][:]))\n",
    "    # 计算子代的个体间的距离值\n",
    "    new_solution= []\n",
    "    for i in range(0,len(non_dominated_sorted_solution2)):\n",
    "        non_dominated_sorted_solution2_1 = [index_of(non_dominated_sorted_solution2[i][j],non_dominated_sorted_solution2[i] ) for j in range(0,len(non_dominated_sorted_solution2[i]))]\n",
    "        #排序\n",
    "        front22 = sort_by_values(non_dominated_sorted_solution2_1[:], crowding_distance_values2[i][:])\n",
    "        front = [non_dominated_sorted_solution2[i][front22[j]] for j in range(0,len(non_dominated_sorted_solution2[i]))]\n",
    "        front.reverse()\n",
    "        for value in front:\n",
    "            new_solution.append(value)\n",
    "            if(len(new_solution)==pop_size):\n",
    "                break\n",
    "        if (len(new_solution) == pop_size):\n",
    "            break\n",
    "    solution = [solution2[i] for i in new_solution]\n",
    "    gen_no = gen_no + 1\n",
    "#Lets plot the final front now\n",
    "function1 = [i * -1 for i in function1_values]\n",
    "function2 = [j * -1 for j in function2_values]\n",
    "plt.xlabel('Function 1', fontsize=15)\n",
    "plt.ylabel('Function 2', fontsize=15)\n",
    "plt.scatter(function1, function2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
